//
//  HMSportSportingViewController.m
//  HMBest22
//
//  Created by Apple on 17/1/9.
//  Copyright © 2017年 Apple. All rights reserved.
//  运动监控控制器

#import "HMSportSportingViewController.h"
#import "HMSportMapViewController.h"
#import "CZAdditions.h"
#import "HMSportSpeaker.h"
#import "HMSportCaptureViewController.h"
@interface HMSportSportingViewController ()<HMSportMapViewControllerDelegate>

@property (nonatomic, strong) HMSportMapViewController *mapViewController;
// 跳转地图的按钮
@property (weak, nonatomic) IBOutlet UIButton *mapButton;

// 距离
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
// 时间
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
// 平均速度
@property (weak, nonatomic) IBOutlet UILabel *avgSpeedLabel;

// 结束按钮的centerX约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *finishBtnCon;
// 继续按钮的centerX约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *continuBtnCon;
// 暂停按钮的centerX约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pauseBtnCon;
// 暂停按钮
@property (weak, nonatomic) IBOutlet UIButton *pauseBtn;
// 继续按钮
@property (weak, nonatomic) IBOutlet UIButton *continueBtn;
// 播报语音的工具类
@property (nonatomic, strong) HMSportSpeaker *sportSpeaker;
@end

@implementation HMSportSportingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.view.backgroundColor = [UIColor redColor];
    
    [self setUpSportSportingVc];
    
    
  
    [self maskLayer];
    
    
    self.sportSpeaker = [[HMSportSpeaker alloc] init];
    // 播放运动的类型
    [self.sportSpeaker speakVoiceWithSportType:self.sportType];
}

/// 设置背景渐变色的代码
- (void)maskLayer
{
    // 创建图层
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    // 设置中心点
    gradientLayer.position = self.view.center;
    // 设置宽高
    gradientLayer.bounds = self.view.bounds;
    // 设置渐变的颜色
    CGColorRef color1 = [UIColor cz_colorWithHex:0x0e1428].CGColor;
    CGColorRef color2 = [UIColor cz_colorWithHex:0x406479].CGColor;
    CGColorRef color3 = [UIColor cz_colorWithHex:0x406578].CGColor;
    gradientLayer.colors = @[(__bridge UIColor *)color1,(__bridge UIColor *)color2,(__bridge UIColor *)color3];
    // 渐变颜色的位置
    gradientLayer.locations = @[@0,@0.6,@1];
    // 插入图层到最下面
    [self.view.layer insertSublayer:gradientLayer atIndex:0];
}
// 即将布局成功后调用
- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    self.mapViewController.mapView.compassOrigin = CGPointMake(self.mapButton.frame.origin.x, self.mapButton.frame.origin.y);
}
/// 添加父子控制器
- (void)setUpSportSportingVc
{
//    for (UIViewController *child in self.childViewControllers) {
//        if ([child isKindOfClass:[HMSportMapViewController class]]) {
//            self.mapVC = (HMSportMapViewController *)child;
//            break;
//        }
//    }
////    // 给地图赋值单次运动的模型
//    self.mapVC.sportTracking = [[HMSportTracking alloc] initWithSportType:self.sportType andSportState:HMSportStateContinue];
    
    // 1.获取地图控制器
    // 1.1获取storyBoard
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"HMSportSporting" bundle:nil];
    // 1.2获取地图控制器
    self.mapViewController = [storyboard instantiateViewControllerWithIdentifier:@"mapViewController"];
    
    // 2.传入模型
    self.mapViewController.sportTracking = [[HMSportTracking alloc] initWithSportType:self.sportType andSportState:HMSportStateContinue];
    
    // 3.设置地图控制器的代理
    self.mapViewController.delegate = self;
}

/// 继续  暂停  结束按钮的点击
- (IBAction)changeState:(UIButton *)sender
{
    self.mapViewController.sportTracking.sportState = sender.tag;
    
    // 点击的按钮是否是暂停按钮  如果是点击了暂停按钮 暂停和继续按钮向左移动  -80
    // 如果点击的不是暂停按钮  暂停和继续按钮向右移动  80
    CGFloat offsetX = (self.pauseBtn == sender) ? -80 : 80;
    
    self.pauseBtnCon.constant += offsetX;
    self.continuBtnCon.constant += offsetX;
    self.finishBtnCon.constant -= offsetX;
    
    
    // 添加动画
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
        
        //如果点击的是暂停按钮  隐藏暂停按钮   如果不是暂停按钮  就显示暂停按钮
        self.pauseBtn.alpha = (self.pauseBtn != sender);
    }];
    
    // 播放运动的状态
    [self.sportSpeaker speakVoiceWithSportState:sender.tag];
}
- (IBAction)showMapViewController
{

    // 1.弹出控制器
    [self presentViewController:self.mapViewController animated:YES completion:nil];
}

// 弹出照相控制器
- (IBAction)showCapture
{
    HMSportCaptureViewController *captureVC = [[HMSportCaptureViewController alloc] init];
    // 模态弹出控制器  两个界面基本没有数据的关系 可以使用模态展示
    [self presentViewController:captureVC animated:YES completion:nil];
    
}

#pragma mark HMSportMapViewControllerDelegate
/// 当用户位置更新时调用    大概1秒调用一次
- (void)userLocationDidUpdate:(HMSportMapViewController *)mapVC
{
    // 给标签赋值(运动数据的绑定)
    
    // 运动的总距离
    self.distanceLabel.text = [NSString stringWithFormat:@"%.02f",mapVC.sportTracking.totalDistance];
    // 运动的总时间
    self.timeLabel.text = mapVC.sportTracking.totalTimeStr;
    // 平均速度
    self.avgSpeedLabel.text = [NSString stringWithFormat:@"%.02f",mapVC.sportTracking.avgSpeed];
    
    // 当'真正'的运动状态为暂停(应该显示继续按钮) 但是界面上还显示暂停按钮  需要点击暂停按钮 去显示继续按钮
    if (mapVC.sportTracking.sportState == HMSportStatePause && self.pauseBtn.alpha != 0) {
        [self changeState:self.pauseBtn];
    }
    
    // 当'真正'的运动状态为继续(应该显示的是暂停按钮)  但是暂停按钮没有显示 需要点击继续按钮 去显示暂停按钮
    if (mapVC.sportTracking.sportState == HMSportStateContinue && self.pauseBtn.alpha ==0) {
        [self changeState:self.continueBtn];
    }
    
    // 播放运动的总距离 总时长 和平均速度
    [self.sportSpeaker speakVoiceWithDistance:mapVC.sportTracking.totalDistance andTime:mapVC.sportTracking.totalTime andAvgSpeed:mapVC.sportTracking.avgSpeed];
}
@end
